# ============================
# 1. 构建阶段（Builder Stage）
# ============================
FROM docker.1ms.run/library/golang:1.24.3-alpine AS builder

ENV CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64 \
    GOPROXY=https://goproxy.cn,direct \
    GO111MODULE=on

WORKDIR /app

# 安装构建依赖
RUN apk add --no-cache git upx

# 预下载依赖，加速构建缓存
COPY go.mod go.sum ./
RUN go mod download

# 复制源码
COPY . .

# 构建参数：版本、提交、构建时间
ARG VERSION=dev
ARG COMMIT=none
ARG BUILD_TIME=unknown

# 编译
RUN go build -ldflags "\
    -s -w \
    -X 'gitee.com/lyuanbo/stock-plus/pkg/version.Version=${VERSION}' \
    -X 'gitee.com/lyuanbo/stock-plus/pkg/version.Commit=${COMMIT}' \
    -X 'gitee.com/lyuanbo/stock-plus/pkg/version.BuildTime=${BUILD_TIME}'" \
    -o app ./cmd/cli && \
    upx --best --lzma app

# ============================
# 2. 运行阶段（Runtime Stage）
# ============================
FROM gcr.io/distroless/static-debian11

USER nonroot:nonroot

WORKDIR /app

COPY --from=builder /app/app ./app
COPY configs/config.prod.yaml ./config.yaml

ENTRYPOINT ["./app"]
